#include "TargetObject.h"
bool TargetObject::compiled = false;

TargetObject::TargetObject()
{
}

TargetObject::~TargetObject()
{
}



void TargetObject::Init()
{
    r = 1;
    g = 1;
    b = 1;
    pos.x = 0;
    pos.z = 0;
    
    do{
       size = (float)(rand()%5)/10;
      }
    while (size > MAX_TARG_SIZE || size < MIN_TARG_SIZE);
    
    
    inte = 0;
    shine = false;
    damage = TARG_DAMAGE;
    intensity = 0;
    CalDim();
    if (!compiled) LoadLists();
    ChooseValidPos();
}

void TargetObject::CalDim()
{
 d100 = TARG_DIM * size;
 d70 = ((TARG_DIM * size) / 100)* 70;
 d35 = ((TARG_DIM * size) /100) * 35    ;
}

void TargetObject::LoadLists()
{
    target_list_name = glGenLists(1);
    glNewList(target_list_name,GL_COMPILE);
       glEnable(GL_COLOR_MATERIAL);
       
       glColor4f(0.50f, 1.0f, 1.0f, 0.3 );     
       glBlendFunc(GL_SRC_ALPHA,GL_ONE);       
       glEnable(GL_BLEND);      
      
       
       glColor3f(0,0,1);
       gluDisk(state->quad_obj,70,100,25,5);
       glColor3f(1,1,0);
       gluDisk(state->quad_obj,35,70,25,5);
       glColor3f(1,0,0);
       gluDisk(state->quad_obj,0,35,25,5);
       glDisable(GL_BLEND);
       
   glEndList();
compiled = true;
}


void TargetObject::Draw()
{
 glPushMatrix();
       glTranslatef(pos.x,pos.y,pos.z);
       glScalef(size,size,size);
       
       if(shine)
       {
        glEnable(GL_COLOR_MATERIAL);
        glColor4f(0.50f, 1.0f, 1.0f, 0.3 );     
       // glBlendFunc(GL_SRC_ALPHA,GL_ONE);       
        glEnable(GL_BLEND);      
      
       
        glColor3f(0,0,b);
        gluDisk(state->quad_obj,70,100,25,5);
        glColor3f(g,g,0);
        gluDisk(state->quad_obj,35,70,25,5);
        glColor3f(r,0,0);
        gluDisk(state->quad_obj,0,35,25,5);
        glDisable(GL_BLEND);
        glDisable(GL_COLOR_MATERIAL);
       }
       else    glCallList(target_list_name); 
 glPopMatrix(); 
}

void TargetObject::ChooseValidPos()
{
   float x,z;
     
   do
   {
     x = (float)(rand()% (int)(HALF_TERRAIN * 2) - HALF_TERRAIN);
     z = (float)(rand()% (int)(HALF_TERRAIN * 2) - HALF_TERRAIN);
   }
   while  ((int)sqrt((x*x ) + (z*z)) > FLY_ZONE_DIST);
 

  pos.x = x;
  pos.y = -state->world->GetHeight(-x,-z);//+TARG_DIM/2;
  pos.z = z;
  
}

bool TargetObject::CheckCollision(int typ,float x,float y,float z)
{
bool ris = false;

type = typ; 
x = -x;
z = -z;

if(z > pos.z - 25 && z < pos.z + 25)
{
 if (y > pos.y - d35 && y < pos.y + d35 
     && x > pos.x - d35 && x < pos.x + d35 )
     {
         intensity = 10;
         ris = true;
     
     }
 else if (y > pos.y - d70 && y < pos.y + d70 
    && x > pos.x - d70 && x < pos.x + d70 )
    {
         intensity = 7;
         ris = true;
    }
 else if (y > pos.y - d100 && y < pos.y + d100 
   && x > pos.x - d100 && x < pos.x + d100 )
   {
         intensity = 3;
         ris = true;
   }
  
  
}
if(ris) Eval_damage();

return ris;
}

void TargetObject::Eval_damage()
{
  inte = intensity;
  switch (type){
      case WEAK_ROKET: intensity *= 500;   break;
      case HEAVY_ROKET:intensity *= 400;   break;
      case BULLET:     intensity *= 1;  ;break;
  }
  
  //segno il punteggio su state
  damage -= intensity;
  state->score += intensity;
  state->fotoid->SetFotoOk();
  shine = true;
  
  if (damage < 0)
  { 
        //genero le esplosioni
    Generate_Explosion(pos.x ,pos.y,pos.z);
    int segno = rand() % 2;
    if (segno == 0) segno = -1; 
    int exps = rand() % 12;
     
    for (int i = 0; i < exps; i++)
    {
         float dx = rand()% 100 * segno;
         float dz = rand()% 100 * segno ; 
         Generate_Explosion(pos.x - dx ,pos.y,pos.z - dz);
    }    
        
   //genero l'esplosione
   shine = false;
   damage = TARG_DAMAGE;
   CalDim();
   intensity = 0;
   
   Init();
  }
}

void TargetObject::DoIdle()
{
  if(shine)
  {
   switch (inte){
      case 10: r = cos(ang * PI_DIV_180);   break;
      case 7:  g = cos(ang * PI_DIV_180);   break;
      case 3:  b = cos(ang * PI_DIV_180);  ;break;
  }
   ang += TARG_INTERVAL_SHINE;
   
   if (ang > TARG_INTERVAL_NUM) ang = 0,shine = false;
  }
}


void TargetObject::Generate_Explosion(float x,float y,float z)
{
   ExplosionObject * s = new ExplosionObject(state);
   s->Init();
   s->setPos(x,y,z);
   
   bool flag = false;
   for (int i = 0;(!flag && i <= state->explosions_counter);i++)
          if (state->explosions[i] == NULL) state->explosions[i] = s,flag = true;

   state->explosions_counter++;
}
